// pages/course/wisdomLife/wisdomLife.js
const app=getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    setInterval:false,
    url:'',
    findAllCoursetypePage:[],
    activeIndex: 0,
    selectcoursebytype:[],
    pageNum:1,
    pageSize:6,
     //根据课程类型并根据收藏量查询降序
 selectcoursebytypeandcollection:[],
 //根据课程类型并根据价格查询升序查询
 selectcoursebytypeandpriceasc:[],
 //根据课程类型并根据价格查询降序查询
 selectcoursebytypeandpricedesc:[],
     courseByPrice:[],
     value1: 0,
    option1: [
      { text: '价格升序', value: 0 },
      { text: '价格降序', value: 1 },
    ],
     //根据课程类型并根据价格区间查询
     selectcoursebytypeandprice:[],
     value2: 'a',
    option2: [
      { text: '免费', value: 'a' },
      { text: '￥50以下', value: 'b' },
      { text: '￥50-100', value: 'c' },
      { text: '￥100-500', value: 'd' },
      { text: '￥500-1000', value: 'e' },
      { text: '￥1000以上', value: 'f' },
    ],
    courseTypeId: 0
  },
  gojingjiangCourse:function(e){
    const id=e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
     //  url: './pages/course/jingjiangCourse?id='+id
     url:'/pages/course/jingjiangCourse/jingjiangCourse?id='+id
    });
  },

//查询所有类型
findAllCoursetypePage(){
  let that = this
  wx.request({
    url: that.data.url+'coursetype/findAllCoursetypePage?pageNum='+that.data.pageNum+'&pageSize='+that.data.pageSize,
    success(resp){
      console.log(resp.data.data.list)
      that.setData({
        findAllCoursetypePage:resp.data.data.list
      })
      }
  })
},
//接收课程类型id
typenameChange(e) {
  const index = e.detail.index;
  this.setData({
    activeIndex: index
  });
  const findAllCoursetypePage = this.data.findAllCoursetypePage;
  const courseTypeId = findAllCoursetypePage[index].courseTypeId;
  console.log(courseTypeId)
  // 将 courseTypeId 存储到全局变量中
  this.setData({
    courseTypeId: courseTypeId
  })
  this.selectcoursebytype(courseTypeId);
  this.selectcoursebytypeandcollection(courseTypeId);
  this.selectcoursebytypeandpriceasc(courseTypeId);
  this.selectcoursebytypeandpricedesc(courseTypeId);
  this.selectcoursebytypeandprice(courseTypeId);
},
//根据课程类型查询课程
selectcoursebytype(courseTypeId){
  let that=this;
  wx.request({
    url: that.data.url+'course/selectcoursebytype?pageNum='+that.data.pageNum+'&pageSize='+that.data.pageSize+'&typeId='+courseTypeId,
    success(resp){
      that.setData({
        selectcoursebytype:resp.data.data.list
      })
      }
  })
},
 //根据课程类型并根据收藏量查询降序
 selectcoursebytypeandcollection(courseTypeId){
   let that = this;
   wx.request({
    url: that.data.url + 'course/selectcoursebytypeandcollection?pageNum=' + that.data.pageNum + '&pageSize=' + that.data.pageSize + '&typeId=' + courseTypeId,
      success(resp) {
        that.setData({
          selectcoursebytypeandcollection: resp.data.data.list
        })
      }
   })
 },
  //根据课程类型并根据价格查询升序查询
  selectcoursebytypeandpriceasc(courseTypeId){
    let that = this;
    wx.request({
      url: that.data.url + 'course/selectcoursebytypeandpriceasc?pageNum=' + that.data.pageNum + '&pageSize=' + that.data.pageSize + '&typeId=' + courseTypeId,
      success(resp) {
        that.setData({
          selectcoursebytypeandpriceasc: resp.data.data.list,
          courseByPrice: resp.data.data.list
        })
      }
    })
  },
  //根据课程类型并根据价格查询降序查询
  selectcoursebytypeandpricedesc(courseTypeId){
    let that = this;
    wx.request({
      url: that.data.url + 'course/selectcoursebytypeandpricedesc?pageNum=' + that.data.pageNum + '&pageSize=' + that.data.pageSize + '&typeId=' + courseTypeId,
      success(resp) {
        that.setData({
          selectcoursebytypeandpricedesc: resp.data.data.list,
          courseByPrice: resp.data.data.list
        })
      }
    })
  },
  // 价格排序改变事件
  onPriceSortChange(e){
    const value = e.detail;
    this.setData({
      value1: value
    });
    if (value == 0) {
      this.selectcoursebytypeandpriceasc(this.data.courseTypeId);
      this.setData({
        courseByPrice:this.data.selectcoursebytypeandpriceasc
      })
    } else {
      this.selectcoursebytypeandpricedesc(this.data.courseTypeId);
      this.setData({
        courseByPrice:this.data.selectcoursebytypeandpricedesc
      })
    }
  },
      //根据课程类型并根据价格区间查询
      selectcoursebytypeandprice(courseTypeId){
        let that = this;
        let min = 0;
          let max = Infinity;
          switch (this.data.value2) {
            case 'a':
              min = 0;
              max = 0;
              break;
            case 'b':
              min = 0;
              max = 50;
              break;
            case 'c':
              min = 50;
              max = 100;
              break;
            case 'd':
              min = 100;
              max = 500;
              break;
            case 'e':
              min = 500;
              max = 1000;
              break;
            case 'f':
              min = 1000;
              max = 2000000;
              break;
          }
          wx.request({
            url: that.data.url + 'course/selectcoursebytypeandprice?pageNum=' + that.data.pageNum + '&pageSize=' + that.data.pageSize + '&typeId=' + courseTypeId + '&min=' + min+'&max='+max,
            success(resp) {
              that.setData({
                selectcoursebytypeandprice: resp.data.data.list
              })
            }
          })
      },
      onPriceRangeChange(e){
        const value = e.detail;
        this.setData({
          value2: value
        });
        const findAllCoursetypePage = this.data.findAllCoursetypePage;
        const courseTypeId = findAllCoursetypePage[this.data.activeIndex].courseTypeId;
        this.selectcoursebytypeandprice(courseTypeId, value);
      },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      url:app.globalData.URL
       })
    this.findAllCoursetypePage();
    this.selectcoursebytype(this.data.courseTypeId);
    this.selectcoursebytypeandcollection(this.data.courseTypeId);
    this.selectcoursebytypeandpriceasc(this.data.courseTypeId);
    this.selectcoursebytypeandpricedesc(this.data.courseTypeId);
    this.selectcoursebytypeandprice(this.data.courseTypeId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

    this.setData({
    selectcoursebytypeandcollection:[],
    selectcoursebytypeandpriceasc:[],
    selectcoursebytypeandpricedesc:[],
    courseByPrice:[],
    selectcoursebytypeandprice:[],
    })
    this.findAllCoursetypePage();
    this.selectcoursebytype(this.data.courseTypeId);
    if(this.data.value1 === 0){
      this.selectcoursebytypeandpriceasc(this.data.courseTypeId);
    }else{
      this.selectcoursebytypeandpricedesc(this.data.courseTypeId);
    }
    this.selectcoursebytypeandcollection(this.data.courseTypeId);
    this.selectcoursebytypeandprice(this.data.courseTypeId);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that = this;
    that.setData({
      pageSize:that.data.pageSize+2,
      setInterval:true
    });
    setTimeout(function () {
      that.setData({
        setInterval:false
      });
        }, 2000); 
        this.findAllCoursetypePage();
    this.selectcoursebytype(this.data.courseTypeId);
    if(this.data.value1 === 0){
      this.selectcoursebytypeandpriceasc(this.data.courseTypeId);
    }else{
      this.selectcoursebytypeandpricedesc(this.data.courseTypeId);
    }
    this.selectcoursebytypeandcollection(this.data.courseTypeId);
    this.selectcoursebytypeandprice(this.data.courseTypeId);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})